package com.example.springbootdemo.controller;

import com.example.springbootdemo.dao.UserMapper;
import com.example.springbootdemo.po.User;
import com.example.springbootdemo.repository.UserRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {

//    @Autowired
//    UserRepository userRepository;

    @Autowired
    UserMapper userMapper;

//    @PostMapping("/upload")
//    @ApiOperation(value="保存用户")
//    @ApiImplicitParam(paramType = "com.example.springbootdemo.ojop.User", name = "user", value="用户", required = true)
//    public List<String> save(@Validated @RequestBody User user, BindingResult result){
//        List<String> errors = new ArrayList();
//        if(result.hasErrors()){
//            List<ObjectError> allErrors = result.getAllErrors();
//            for(ObjectError error: allErrors){
//                errors.add(error.getDefaultMessage());
//            }
//        }
//        if(errors.size() == 0){
//            userRepository.save(user);
//        }
//
//        return errors;
//    }

    @GetMapping("/selectById")
    public User selectUserByPrimaryKey(@RequestParam Integer id){
        return userMapper.selectByPrimaryKey(id);
    }
}
